package com.tools.module.sys.service.impl;

import com.tools.common.dynamicquery.DynamicQuery;
import com.tools.common.model.PageBean;
import com.tools.common.model.Result;
import com.tools.common.util.DateUtils;
import com.tools.common.util.ShiroUtils;
import com.tools.module.sys.entity.SysProject;
import com.tools.module.sys.repository.SysProjectRepository;
import com.tools.module.sys.repository.SysUserRepository;
import com.tools.module.sys.service.SysProjectService;
import com.tools.module.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysProjectServiceImpl implements SysProjectService {

    @Autowired
    private DynamicQuery dynamicQuery;
    @Autowired
    private SysProjectRepository sysProjectRepository;
    @Autowired
    private SysUserService sysUserService;



    @Override
    public Result save(SysProject sysProject) {
        if(ShiroUtils.getUserId().equals(null)){
            return Result.error("都没登录还想查询吗？想太多了");
        }
        sysProject.setUserId(ShiroUtils.getUserId());
        sysProject.setGmtCreate(DateUtils.getTimestamp());
        sysProjectRepository.saveAndFlush(sysProject);
        return Result.ok("保存成功");
    }

    @Override
    public List<SysProject> select(String parentCode) {
        return null;
    }

    @Override
    public Result get(Long id) {
        return null;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(Long id) {
        sysProjectRepository.deleteById(id);
        return Result.ok("删除成功");
    }


    @Override
    public Result list(SysProject sysProject) {
        String nativeSql = "SELECT COUNT(*) FROM sys_project";
        nativeSql += common(sysProject);
        Long count = dynamicQuery.nativeQueryCount(nativeSql);
        PageBean<SysProject> data = new PageBean<>();
        if(count>0){
            nativeSql = "SELECT * FROM sys_project ";
            nativeSql += common(sysProject);
            if("".equals(common(sysProject))){
                nativeSql += " WHERE user_id = '" + ShiroUtils.getUserId()+ "' ";
            }
            nativeSql += "ORDER BY gmt_create desc";
            Pageable pageable = PageRequest.of(sysProject.getPageNo(),sysProject.getPageSize());
            List<SysProject> list =  dynamicQuery.nativeQueryPagingList(SysProject.class,pageable,nativeSql);
            data = new PageBean(list,count);
        }
        return Result.ok(data);
    }

    public String common(SysProject sysProject){
        String description = sysProject.getDescription();
        String commonSql = "";
        if(StringUtils.isNotBlank(description) || StringUtils.isNotBlank(sysProject.getTbOrder())
                || StringUtils.isNotBlank(sysProject.getPhone()) || !String.valueOf(sysProject.getUserStatus()).equals("null")
                || !String.valueOf(sysProject.getUserStatus()).equals("null") || !String.valueOf(sysProject.getYearStatus()).equals("null")){
            if(ShiroUtils.getUserId().intValue() != 1){
                commonSql += " WHERE username like '%"+description+"%' and user_id = '" + ShiroUtils.getUserId()+ "'";
            }else {
                commonSql += " WHERE username like '%"+description+"%' ";
            }
            if(StringUtils.isNotBlank(sysProject.getTbOrder())){
                commonSql += " AND tb_order like '%"+sysProject.getTbOrder()+"%' ";
            }
            if(StringUtils.isNotBlank(sysProject.getPhone())){
                commonSql += " AND phone like '%"+sysProject.getPhone()+"%' ";
            }
            if(!String.valueOf(sysProject.getStatus()).equals("null")){
                commonSql += " AND status = '"+sysProject.getStatus()+"' ";
            }
            if(!String.valueOf(sysProject.getUserStatus()).equals("null")){
                commonSql += " AND user_status = '"+sysProject.getUserStatus()+"' ";
            }
            if(!String.valueOf(sysProject.getYearStatus()).equals("null")){
                commonSql += " AND year_status = '"+sysProject.getYearStatus()+"'";
            }

        }
        return commonSql;
    }
}
